#include "../../../lib/std.mi"

/*--------------------------------------------------------------------
 Magnifier script
--------------------------------------------------------------------*/

Class Layer Lens;

Global Int last_x, last_y;
Function Lens makeNewLens(Layout l);

Global List layout_list;
Global List Fx_list;
Global Timer mTimer;
Global Slider PrecisionSlider;
Global Int Precision;

Global Lens Fx1, Fx2, Fx3, Fx4, Fx5, Fx6, Fx7, Fx8, Fx9, Fx10;

Class Timer Magnifier;
Function int initFx(Layer _Fx);

Lens.fx_onGetPixelD(double r, double d, double x, double y) {
  if (d < 0.6) return d*(sin(d)*2);
  return d;
}

Lens.fx_onGetPixelA(double r, double d, double x, double y) {
  return cos(d*3.14159/1.25);
}

System.onScriptUnloading() {
  delete mTimer;
  int i;
  for (i=0;i<fx_list.getNumItems();i++) {
    Layer l = fx_list.enumItem(i);
    delete l;
  }
  delete Fx_list;
  delete layout_list;
}

System.onScriptLoaded() {

  PrecisionSlider = getContainer("magnifier.config").getLayout("normal").getObject("magnifier.config.slider");
  Precision = getPrivateInt("magnifier", "precision", 10);
  float f = 24-Precision;
  PrecisionSlider.setPosition((f / 24.0) * 255.0);

  mTimer = new Timer;
  mTimer.setDelay(20);
  if (Precision != 0) mTimer.start();

  layout_list = new List;

  int i, j, c;
  Fx_list = new List;

  for (i=0;i<getNumContainers();i++) {
    if (!enumContainer(i).isDynamic()) 
     for (j=0;j<enumContainer(i).getNumLayouts();j++) {
      c = c + 1;
      if (c <= 10) {
        Layout l = enumContainer(i).enumLayout(j);
        layout_list.addItem(l);
        // eeek, i definitly need arrays, and switch... or maybe i need to make objects residing inside lists trap events, hmm..
        Layer la = makeNewLens(l);

        if (c == 1) Fx1 = la;
        if (c == 2) Fx2 = la;
        if (c == 3) Fx3 = la;
        if (c == 4) Fx4 = la;
        if (c == 5) Fx5 = la;
        if (c == 6) Fx6 = la;
        if (c == 7) Fx7 = la;
        if (c == 8) Fx8 = la;
        if (c == 9) Fx9 = la;
        if (c == 10) Fx10 = la;
      }
     }
  }
}

mTimer.onTimer() {
  int i;
  if (getMousePosX() == last_x && getMousePosY() == last_y) return;
  for (i=0;i<layout_list.getNumItems();i++) {
    Layout l = layout_list.enumItem(i);
    int x = l.getMousePosX();
    int y = l.getMousePosY();
    Layer thisFx = Fx_list.enumItem(i);
    if (x < -50 || x > l.getWidth()+50 || y < -50 || y > l.getHeight()+50) {
      if (thisFx.fx_getEnabled())
        thisFx.fx_setEnabled(0);
    } else {
      if (!thisFx.fx_getEnabled())
        thisFx.fx_setEnabled(1);
    thisFx.resize(x-50, y-50, 100, 100);
    }
  last_x = getMousePosx();
  last_y = getMousePosY();
  }
}

int initFx(Layer _Fx) {
  Fx_list.addItem(_Fx);
  _Fx.fx_setBgFx(1);
  _Fx.fx_setWrap(0);
  _Fx.fx_setBilinear(1);
  _Fx.fx_setGridSize(Precision,Precision);
  _Fx.fx_setRect(0);
  _Fx.fx_setClear(0);
  _Fx.fx_setLocalized(1);
  _Fx.fx_setRealtime(0);
  _Fx.fx_setAlphaMode(1);
  return 0;
}

Lens makeNewLens(Layout l) {
  Lens la = new Layer;
  la.setXmlParam("x", "-51");
  la.setXmlParam("y", "-51");
  la.setXmlParam("image", "script.magnifier.transparent50x50");
  la.setXmlParam("ghost", "1");
  la.init(l);
  initFx(la);
  return la;
}

PrecisionSlider.onSetPosition(int p) {
  float f = p;
  p = 24-((f / 255.0) * 24.0);
  for (int i=0;i<fx_list.getNumItems();i++) {
    Lens fx = fx_list.enumItem(i);
    fx.fx_setGridSize(p, p);
    if (p == 0) {
      fx.fx_setEnabled(0);
      mTimer.stop();
    } else {
      if (Precision == 0)
        mTimer.start();
      if (fx.fx_getEnabled()) {
        fx.fx_setEnabled(0);
        fx.fx_setEnabled(1);
      }
    }
  }
  setPrivateInt("magnifier", "precision", p);
  Precision = p;
}
